<?php
/**
 * Created by PhpStorm.
 * Author: sitenv@aliyun.com
 * CreateTime: 2020/11/24 11:26
 * Blog：www.myblogs.xyz
 */

require_once dirname(__DIR__) . '/autoload.php';

class Push
{
    protected $app_key = '';
    protected $master_secret = '';
    protected $client;
    protected $cid;

    public function __construct($app_key, $master_secret)
    {
        $this->app_key = $app_key;
        $this->master_secret = $master_secret;
        $this->client = new JPush\Client($app_key, $master_secret);
    }

    /**
     * @param $data
     * @return mixed
     */
    protected function json_array($data)
    {
        return json_decode(json_encode($data),true);
    }

    /**
     * 开发模式
     * @param string $alert
     * @param string $alias
     * @param array $ios_notification
     * @param array $android_notification
     * @param string $form
     * @return mixed
     */
    public function develop($alert = '', $alias = '', $ios_notification = [], $android_notification = [], $form = 'all')
    {
        $result = $this->client->push()
            ->setPlatform($form)
            ->addAlias($alias)
            ->iosNotification($alert, $ios_notification)
            ->androidNotification($alert, $android_notification)
            ->options(['apns_production'=>false])
            ->send();
        return $this->json_array($result);
    }

    /**
     * 生产模式
     * @param string $alert
     * @param string $alias
     * @param array $ios_notification
     * @param array $android_notification
     * @param string $form
     * @return mixed
     */
    public function production($alert = '', $alias = '', $ios_notification = [], $android_notification = [], $form = 'all')
    {
        $result = $this->client->push()
            ->setPlatform($form)
            ->addAlias($alias)
            ->iosNotification($alert, $ios_notification)
            ->androidNotification($alert, $android_notification)
            ->options(['apns_production'=>true])
            ->send();
        return $this->json_array($result);
    }

}